package orangeScript.android.helper;


import orangeScript.language.FunctionValue;
import orangeScript.language.NumberValue;
import orangeScript.language.RawValue;
import orangeScript.language.StringValue;
import orangeScript.language.Value;
import orangeScript.language.exceptions.UserDefinedException;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

public class OnClickListenerImpl implements OnClickListener {
	private final FunctionValue fv;
	
	public OnClickListenerImpl(Value fv) {
		if ((fv == null) || !(fv instanceof FunctionValue)) {
			throw new UserDefinedException(new StringValue("Invalid arguments")); 
		}
		this.fv = (FunctionValue)fv;
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		Value[] args = new Value[ ] { RawValue.get(dialog), NumberValue.getNumber(which)};
		fv.call(args, null);
	}
	
	public static OnClickListener get(Value fv) {
		return new OnClickListenerImpl(fv);
	}
}
